

#pragma once

#include "SocketBase.hpp"
#include <string>

CYBERTRON_BEGIN

class SocketTcpServerImpl;

class SocketTcpServer : public std::enable_shared_from_this<SocketTcpServer>
{
public:
	CybertronCoreAPI SocketTcpServer();
	CybertronCoreAPI virtual ~SocketTcpServer();

	CybertronCoreAPI bool listen(
		int port, 
		int numThreads = -1);
	CybertronCoreAPI virtual void close();

	CybertronCoreAPI size_t getNumClients() const;

	CybertronCoreAPI unsigned short  getListenPort();
	CybertronCoreAPI unsigned int getMaximalIdleMilliseconds() const;
	CybertronCoreAPI void setMaximalIdleMilliseconds(unsigned int maxIdleMilliseconds);

	// performance warning: this is slow!
	CybertronCoreAPI void broadcast(const Message& msg);
    CybertronCoreAPI void broadcast(const std::string& msg);

protected:
	CybertronCoreAPI virtual void onAccept(SocketBasePtr pRemoteSocket);

private:
	void doAccept();
	void doClearIdle();

	void closeThis();

private:
	SocketTcpServerImpl* mpImpl;
};

CYBERTRON_END
